package com.hi.hiparty.ui;

import java.util.Map;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.hi.hiparty.R;
import com.hi.hiparty.entity.Party;
import com.hi.hiparty.service.ILocationService;
import com.hi.hiparty.ui.fragment.EditPartyFragment;
import com.hi.hiparty.ui.fragment.EditPartyFragment.OnClickEditLocationListener;
import com.hi.hiparty.ui.fragment.EditPartyFragment.OnPubNewPartySuccessListener;
import com.hi.hiparty.ui.fragment.EditUserFragment;
import com.hi.hiparty.ui.fragment.LocationInputFragment;
import com.hi.hiparty.ui.fragment.PartyDetailFragment;
import com.hi.hiparty.ui.fragment.PartyListFragment;
import com.hi.hiparty.ui.fragment.PartyListFragment.OnPartyListItemClickListener;
import com.hi.hiparty.util.Constants;
import com.hi.hiparty.util.PictureHandler;

public class MainActivity extends SherlockFragmentActivity implements OnCheckedChangeListener,
		OnClickEditLocationListener, OnPubNewPartySuccessListener, OnPartyListItemClickListener {

	private static final String TAG = MainActivity.class.getSimpleName();

	private String FRAGMENT_TAG_SQUARE = "fragment_tag_square";

	private ILocationService locationService;

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			locationService = (ILocationService) service;
			Log.v(TAG, "on serivce connected, count is " + locationService.getLatlng());
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			locationService = null;
		}

	};

	private OnItemClickListener onLocationSelectedListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) {
			Object item = listView.getItemAtPosition(position);
			if (null != item) {
				Map<String, String> locMap = (Map<String, String>) item;
				EditPartyFragment pubDateFragment = (EditPartyFragment) getSupportFragmentManager().findFragmentByTag(
						Constants.FRAGMENT_TAG_PUB_PARTY);
				if (null != pubDateFragment) {
					String address = locMap.get("name");
					String latlng = locMap.get("latlng");
					pubDateFragment.setLocation(address, latlng);
					FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
					ft.replace(R.id.main_fragment_container, pubDateFragment, Constants.FRAGMENT_TAG_PUB_PARTY);
					ft.addToBackStack(null);
					ft.commit();
				}
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.main_fragment_container, new PartyListFragment(), Constants.FRAGMENT_TAG_LIST_PARTY)
					.commit();
		}

		getApplicationContext().bindService(new Intent("com.hi.hiparty.service.LocationService"), serviceConnection,
				BIND_AUTO_CREATE);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private void changeToFragment(SherlockFragment fragment, String tag) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.main_fragment_container, fragment, tag);
		ft.addToBackStack(null);
		ft.commit();
	}

	public void onPubNewParty() {
		EditPartyFragment pubNewPartyFragment = new EditPartyFragment();
		pubNewPartyFragment.setOnClickEditLocationListener(this);
		changeToFragment(pubNewPartyFragment, Constants.FRAGMENT_TAG_PUB_PARTY);
	}

	public void onEditUser() {
		EditUserFragment eU = new EditUserFragment();
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.main_fragment_container, eU, Constants.FRAGMENT_TAG_EDIT_USER);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void onClickEditLocation() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		LocationInputFragment locationInputFragment = new LocationInputFragment();
		locationInputFragment.setLocationService(locationService);
		locationInputFragment.setOnItemClickListener(onLocationSelectedListener);
		ft.replace(R.id.main_fragment_container, locationInputFragment, Constants.FRAGMENT_TAG_EDIT_LOCATION);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radio_button0:
			break;
		case R.id.radio_button1:
			break;
		case R.id.radio_button2:
			break;
		case R.id.radio_button3:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		if (null != serviceConnection) {
			getApplicationContext().unbindService(serviceConnection);
		}
		super.onDestroy();
	}

	@Override
	public void onPubNewPartySuccess() {
		EditPartyFragment pubDateFragment = (EditPartyFragment) getSupportFragmentManager().findFragmentByTag(
				Constants.FRAGMENT_TAG_PUB_PARTY);
		if (null != pubDateFragment) {
			getSupportFragmentManager().beginTransaction().remove(pubDateFragment).commit();
		}
		PartyListFragment partyListFragment = (PartyListFragment) getSupportFragmentManager().findFragmentByTag(
				Constants.FRAGMENT_TAG_LIST_PARTY);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_fragment_container, partyListFragment, Constants.FRAGMENT_TAG_LIST_PARTY).commit();
		partyListFragment.refresh();
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// super.onActivityResult(requestCode, resultCode, data)
		PictureHandler picHandler = PictureHandler.getInstance("picHandler");
		if(null!=picHandler)
			picHandler.onResult(requestCode, resultCode, data, this);

	}

	@Override
	public void onPartyListItemClick(Party party) {
		// TODO Auto-generated method stub
		PartyDetailFragment partyDetailFragment = new PartyDetailFragment();
		partyDetailFragment.setParty(party);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.main_fragment_container, partyDetailFragment, Constants.FRAGMENT_PARTY_DETAIL);
		ft.addToBackStack(null);
		ft.commit();
	}
}
